


export class CommonUtil {
	TYPE_NOT_SET = 'TYPE_NOT_SET';
	TYPE_EVERY = 'TYPE_EVERY';
	TYPE_RANGE = 'TYPE_RANGE';
	TYPE_LOOP = 'TYPE_LOOP';
	TYPE_WORK = 'TYPE_WORK';
	TYPE_LAST = 'TYPE_LAST';
	TYPE_SPECIFY = 'TYPE_SPECIFY';
	DEFAULT_VALUE = '*'

	valueList = [];
	valueRange = {
		start: 0,
		end: 2,
	};
	valueLoop = {
		start: 0,
		interval: 2,
	}
	valueWork = 1;
	valueLast = 1;
	type = this.TYPE_EVERY;
	limitDecimals = value => {
		return value && String(value).replace(/^[^\d]+/g, '');
	}


	limitStr = (i: number) => i > 9 ? String(i) : '0' + i;
	secondSpecifyOpt = Array.from(Array(60)).map((it, i) => { return { label: this.limitStr(i), value: this.limitStr(i), checked: false } });;
	minuteSpecifyOpt = Array.from(Array(60)).map((it, i) => { return { label: this.limitStr(i), value: this.limitStr(i), checked: false } });;
	hourSpecifyOpt = Array.from(Array(24)).map((it, i) => { return { label: this.limitStr(i), value: this.limitStr(i), checked: false } });;
	daySpecifyOpt = Array.from(Array(24)).map((it, i) => { return { label: this.limitStr(i + 1), value: this.limitStr(i + 1), checked: false } });;
	weekSpecifyOpt = Array.from(Array(7)).map((it, i) => { return { label: this.limitStr(i + 1), value: this.limitStr(i + 1), checked: false } });;
	monthSpecifyOpt = Array.from(Array(12)).map((it, i) => { return { label: this.limitStr(i + 1), value: this.limitStr(i + 1), checked: false } });;



	getValue() {
		let result = []
		switch (this.type) {
			case this.TYPE_NOT_SET:
				result.push('?')
				break
			case this.TYPE_EVERY:
				result.push('*')
				break
			case this.TYPE_RANGE:
				result.push(`${this.valueRange.start}-${this.valueRange.end}`)
				break
			case this.TYPE_LOOP:
				result.push(`${this.valueLoop.start}/${this.valueLoop.interval}`)
				break
			case this.TYPE_WORK:
				result.push(`${this.valueWork}W`)
				break
			case this.TYPE_LAST:
				result.push(`${this.valueLast}L`)
				break
			case this.TYPE_SPECIFY:
				let res = this.valueList.filter(it => it.checked).map(it => Number(it.value)).join(',');
				if (!res) {
					this.valueList[0].checked = true;
					res = Number(this.valueList[0].value).toString();
				}
				result.push(res);
				break
			default:
				result.push(this.DEFAULT_VALUE)
				break
		}
		return result.length > 0 ? result.join('') : this.DEFAULT_VALUE
	}


	init(value) {
		try {
			if (!value || value === this.DEFAULT_VALUE) {
				this.type = this.TYPE_EVERY
			} else if (value.indexOf('?') >= 0) {
				this.type = this.TYPE_NOT_SET
			} else if (value.indexOf('-') >= 0) {
				this.type = this.TYPE_RANGE
				const values = value.split('-')
				if (values.length >= 2) {
					this.valueRange.start = parseInt(values[0])
					this.valueRange.end = parseInt(values[1])
				}
			} else if (value.indexOf('/') >= 0) {
				this.type = this.TYPE_LOOP
				const values = value.split('/')
				if (values.length >= 2) {
					this.valueLoop.start = value[0] === '*' ? 0 : parseInt(values[0])
					this.valueLoop.interval = parseInt(values[1])
				}
			} else if (value.indexOf('W') >= 0) {
				this.type = this.TYPE_WORK
				const values = value.split('W')
				if (!values[0] && !isNaN(values[0])) {
					this.valueWork = parseInt(values[0])
				}
			} else if (value.indexOf('L') >= 0) {
				this.type = this.TYPE_LAST
				const values = value.split('L')
				this.valueLast = parseInt(values[0])
			} else if (value.indexOf(',') >= 0 || !isNaN(value)) {
				this.type = this.TYPE_SPECIFY;
				const res = value.split(',').map(i => this.limitStr(i));
				this.valueList.forEach(it => {
					it.checked = res.includes(it.value)
				});
				// this.valueList = value.split(',').map(item => parseInt(item))
			} else {
				this.type = this.TYPE_EVERY
			}
		} catch (e) {
			// console.info(e)
			this.type = this.TYPE_EVERY;
		}
	}
}